package com.fulihui.information.biz.conv;

import com.fulihui.information.dal.dataobj.InfoCategory;
import com.fulihui.information.dto.CategoryDTO;
import com.fulihui.information.enums.EYesOrNo;
import com.fulihui.information.request.CategorySaveRequest;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;
import static org.near.toolkit.common.StringUtil.EMPTY_STRING;
import static org.near.toolkit.common.StringUtil.defaultIfBlank;
import static org.springframework.util.CollectionUtils.isEmpty;

/**
 * 类目装换器
 * @author Willard.Hu on 2016/12/27 0027.
 */
public class CategoryConv {

    public static InfoCategory toDataObj(CategorySaveRequest request) {
        if (request == null) {
            return null;
        }
        InfoCategory record = new InfoCategory();
        record.setCode(request.getCode());
        record.setName(request.getName());
        record.setParentCode(request.getParentCode());
        record.setProductCode(request.getProductCode());
        if (request.getShowAdvert() != null) {
            record.setShowAdvert(request.getShowAdvert() ? EYesOrNo.YES.getCode() : EYesOrNo.NO.getCode());
        }
        if (request.getDisplay() != null) {
            record.setDisplay(request.getDisplay() ? EYesOrNo.YES.getCode() : EYesOrNo.NO.getCode());
        }
        record.setHref(request.getHref());
        return record;
    }

    public static CategoryDTO toDTO(InfoCategory category) {
        if (category == null) {
            return null;
        }
        CategoryDTO dto = new CategoryDTO();
        dto.setCode(category.getCode());
        dto.setName(category.getName());
        dto.setParentCode(category.getParentCode());
        dto.setProductCode(category.getProductCode());
        dto.setShowAdvert(EYesOrNo.YES.getCode().equals(category.getShowAdvert()));
        dto.setHref(category.getHref());
        dto.setDisplay(EYesOrNo.YES.getCode().equals(category.getDisplay()));
        dto.setSortIndex(category.getSortIndex());
        dto.gmtCreate(category.getGmtCreate());
        dto.gmtModified(category.getGmtModified());
        return dto;
    }

    public static List<CategoryDTO> toList(List<InfoCategory> list, boolean format) {
        if (isEmpty(list)) {
            return emptyList();
        }
// FIXME Remove
//        List<CategoryDTO> dtoList;
//        if (format) {
//            dtoList = new LinkedList<>();
//            String nextCode = null;
//            Optional<InfoCategory> optional;
//            int n = 0;
//            while ((optional = find(nextCode, list)).isPresent() && n != list.size()) {
//                nextCode = optional.get().getCode();
//                if (isBlank(nextCode)) {
//                    n++;
//                }
//                dtoList.add(0, toDTO(optional.get()));
//            }
//            return dtoList;
//        } else {
//            dtoList = list.stream().map(CategoryConv::toDTO).collect(Collectors.toList());
//        }
//        return dtoList;
        return list.stream().map(CategoryConv::toDTO).collect(Collectors.toList());
    }

    private static Optional<InfoCategory> find(String nextCode, List<InfoCategory> list) {
        if (isEmpty(list)) {
            return Optional.empty();
        }
        for (InfoCategory it : list) {
            String a = defaultIfBlank(it.getNextCode(), EMPTY_STRING);
            String b = defaultIfBlank(nextCode, EMPTY_STRING);
            if (a.equals(b)) {
                return Optional.of(it);
            }
        }
        return Optional.empty();
    }
}
